package com.yiboshi.rehrs.config;

import com.yiboshi.rehrs.common.VarConstant;
import com.yiboshi.rehrs.domain.entity.SmZhongUser;
import com.yiboshi.rehrs.domain.entity.ZhongUser;
import com.yiboshi.rehrs.param.smurfs.UserInfo;
import org.slf4j.MDC;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * @author jack 2025/2/22
 */
public class ThreadLocalSession {


    private static final ThreadLocal<Map<String, Object>> mapThreadLocal =
            ThreadLocal.withInitial(HashMap::new);

    public static void put(String key, Object value) {
        mapThreadLocal.get().put(key, value);
    }
    public static <T> T get(String key) {
        return Optional.ofNullable(mapThreadLocal.get()).map(m -> (T) (m.get(key))).orElse(null);
    }

    public static Integer getMode(){
        return get("mode");
    }

    public static boolean isMode(int mode){
        return Objects.equals(get("mode"),mode);
    }

    public static void setMode(Integer mode){
        put("mode",mode);
    }

    public static SmZhongUser getSmUserInfo(){
        return get(VarConstant.SM_USER_SESSION_KEY);
    }
    public static void setSmUserInfo(ZhongUser userInfo){
        put(VarConstant.SM_USER_SESSION_KEY,userInfo);
    }

    public static String getToken() {
        return Optional.ofNullable(getSmUserInfo()).map(ZhongUser::getToken).orElse(null);
    }


  /*  public static String getAuthorization() {
        return get(VarConstant.Authorization);
    }

    public static void setAuthorization(String authorization) {
        put(VarConstant.Authorization,authorization);
    }*/






    public static void clear() {
        mapThreadLocal.remove();
        MDC.clear();
    }

    public static void setHeaderAuthValid(boolean headerAuthValid) {
        put("headerAuthValid",headerAuthValid);
    }

    public static boolean getHeaderAuthValid() {
        Object valid = get("headerAuthValid");
        return Objects.equals(true,valid);
    }
}
